<?php


class OrganizeService extends TP_Service {
    private $role;
    public function __construct() {
        parent::__construct();
        $this->load->model("SModel/Organize_model");
        $this->load->service("PService/UploadService");
        $this->load->service("PService/ExportExcelService");
        $this->role = $this->getRole($_SESSION[SESS_UUID]);
    }




    // 能否操作
    private function hasPre() {
        if ($this->role->role_id != WebRole::Manage[TPEnum::Value]) {
            exit(retJson(ErrCode::ErrCode_10404));
        }
    }



    /**
     * 方法名 organizeList
     *
     * @Name 获取方法列和角色方法列
     * @Desc 组织列表
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一e
     * @return      [type]                   [description]
     */
    public function organizeList($param, $export = false) {
        if (!$export) {
            $param = array_filter($param, 'filtrfunction');
            if (!isset($param['pageNo'], $param['pageSize'])) {
                exit(retJson(ErrCode::ErrCode_10002));
            }

            $conarr['pageNo'] = $param['pageNo'];
            $conarr['pageSize'] = $param['pageSize'];
        }



        $conarr['items'] = "id, dptmt_name, org_name, status";
        $conarr['orderby'] = "id asc";



        if ($export) {
            $list = $this->Organize_model->get_list_full($conarr);
        } else {
            $list = $this->Organize_model->get_page_list($conarr);
            foreach ($list['ret'] as &$value) {
                $value['statusInfo'] = $value['status'] == EnableState::Enable[TPEnum::Value] ? "已启用" : "已停用";
            }
        }
        return $list;
    }




    /**
     * 方法名 organizeDelete
     *
     * @Name 获取方法列和角色方法列
     * @Desc 组织删除
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     */
    public function organizeDelete($param) {
        if (!isset($param['oid']) || empty($param['oid'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $this->hasPre();

        $this->Organize_model->delete(['id' => $param['oid']]);
        return true;
    }




    /**
     * 方法名 organizeEdit
     *
     * @Name 获取方法列和角色方法列
     * @Desc 组织修改
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2021-11-15 09:30:37 星期一
     * @return      [type]                   [description]
     *
     */
    public function organizeEdit($param) {
        if (!isset($param['dptmt_name'], $param['org_name'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        $this->hasPre();


        $date = [
            'dptmt_name' => $param['dptmt_name'],
            'org_name'   => $param['org_name'],
            'created'    => time(),
        ];


        if (!empty($param['id'])) {
            $date['updated'] = time();
            $this->Organize_model->update($date, ['id' => $param['id']]);
        } else {
            $date['created'] = time();
            $this->Organize_model->add($date);
        }

        return true;
    }




    /**
     * 方法名 organizeImport
     *
     * @Name dealStock
     * @Desc 组织导入
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      zn
     * @Date 2022-02-17 14:11:28
     * @return      [type]                   [description]
     */
    public function organizeImport() {
        $this->hasPre();

        if (empty($_FILES)) {
            exit(retJson(ErrCode::ErrCode_10601));
        }


        $path = $this->UploadService->UploadFile('organize', true);
        $excellist =  $this->ExportExcelService->importexcel($path, 'B', 'A', 2);
        if (count($excellist) == 0) {
            exit(retJson(ErrCode::ErrCode_10602));
        }

        //去掉空数组
        foreach ($excellist as $key => &$value) {
            $value = array_filter($value, 'filtrfunction');
        }
        $excellist = array_filter($excellist);

        foreach ($excellist as $v) {
            $add[] = array(
                'dptmt_name' => $v['A'] ?? '',
                'org_name'   => $v['B'] ?? '',
                'created'    => time(),
            );
        }
        $this->Organize_model->add($add);
        return true;
    }




    /**
     * 方法名 orderStatus
     *
     * @Name orderStatus
     * @Desc 组织状态
     * @OpenFlag 是否开放,1开放0权限控制
     * @Author      DJC
     * @Date 2022-02-17 14:11:28
     * @return      [type]                   [description]
     */
    public function organizeStatus($param) {
        $this->hasPre();

        if (!isset($param['oid'], $param['status'])) {
            exit(retJson(ErrCode::ErrCode_10002));
        }

        if (!in_array($param['status'], [0, 1])) {
            exit(retJson(ErrCode::ErrCode_10901));
        }

        $this->Organize_model->update(['status' => $param['status']], ['id' => $param['oid']]);
        return true;
    }




    /**
     * 方法名 function
     * 
    * @Name 获取方法列和角色方法列
    * @Desc 方法描述
    * @OpenFlag 是否开放,1开放0权限控制
    * @Author      DJC
    * @Date 2022-03-14 10:08:28
    * @return      [type]                   [description]
    */
    public function dptmtList(){
        $conarr['items'] = "dptmt_name";
        $conarr['groupby'] = "dptmt_name";

        $res = $this->Organize_model->get_list_full($conarr);

        return $res;
    }



    public function orgList($param){
        if(!isset($param['dptmt_name'])){
            exit(retJson(ErrCode::ErrCode_10002));

        }
        $conarr['items'] = "org_name";
        $conarr['groupby'] = "org_name";
        $conarr['where'] = sql_format("dptmt_name = '{0}'",$param['dptmt_name']);

        $res = $this->Organize_model->get_list_full($conarr);

        return $res;
    }



    
}
